package com.lxmes.hr.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.*;
import com.lxmes.hr.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.service.INcAttendDetailService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 考勤明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcAttendDetailServiceImpl implements INcAttendDetailService {
    @Autowired
    private NcAttendDetailMapper ncAttendDetailMapper;

    @Autowired
    protected Validator validator;

    @Autowired
    private NcSchedulingRulesMapper ncSchedulingRulesMapper;

    @Autowired
    private NcPersonnelInfoMapper ncPersonnelInfoMapper;

    @Autowired
    private NcOriginalAttendMapper originalAttendMapper;

    @Autowired
    private NcHolidayManageMapper ncHolidayManageMapper;

    @Autowired
    private NcWorkOverCalculateMapper ncWorkOverCalculateMapper;

    @Autowired
    private NcAttendAbnormalMapper ncAttendAbnormalMapper;

    /**
     * 查询考勤明细
     *
     * @param id 考勤明细主键
     * @return 考勤明细
     */
    @Override
    public NcAttendDetail selectNcAttendDetailById(Long id) {
        return ncAttendDetailMapper.selectNcAttendDetailById(id);
    }

    /**
     * 查询考勤明细列表
     *
     * @param ncAttendDetail 考勤明细
     * @return 考勤明细
     */
    @Override
    public List<NcAttendDetail> selectNcAttendDetailList(NcAttendDetail ncAttendDetail) {
        return ncAttendDetailMapper.selectNcAttendDetailList(ncAttendDetail);
    }

    /**
     * 新增考勤明细
     *
     * @param ncAttendDetail 考勤明细
     * @return 结果
     */
    @Override
    public int insertNcAttendDetail(NcAttendDetail ncAttendDetail) {
        ncAttendDetail.setCreateTime(DateUtils.getNowDate());
        return ncAttendDetailMapper.insertNcAttendDetail(ncAttendDetail);
    }

    /**
     * 修改考勤明细
     *
     * @param ncAttendDetail 考勤明细
     * @return 结果
     */
    @Override
    public int updateNcAttendDetail(NcAttendDetail ncAttendDetail) {
        ncAttendDetail.setUpdateTime(DateUtils.getNowDate());
        return ncAttendDetailMapper.updateNcAttendDetail(ncAttendDetail);
    }

    /**
     * 批量删除考勤明细
     *
     * @param ids 需要删除的考勤明细主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendDetailByIds(Long[] ids) {
        return ncAttendDetailMapper.deleteNcAttendDetailByIds(ids);
    }

    /**
     * 删除考勤明细信息
     *
     * @param id 考勤明细主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendDetailById(Long id) {
        return ncAttendDetailMapper.deleteNcAttendDetailById(id);
    }

    @Override
    public String imporIteminfo(List<NcAttendDetail> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAttendDetail org : userList) {
            try {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcAttendDetail(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工姓名 " + org.getEmployeeName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + org.getEmployeeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /*
     *每月最后一天跑  班次为2次
     */
    @Override
    @Transactional
    public int attendanceStatistics(String type) {

        //1:循环本月的天数从一号到最后一天
//        String nyDate = DateUtils.getMonth();
        String nyDate = "2023-03";
        ncAttendDetailMapper.deleteNcAttendDetailByNyDate(nyDate);

        String[] split = nyDate.split("-");
        Integer manyNum = DateUtils.calDayNumbersByMonth(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        for (int i = 1; i <= manyNum; i++) {
            String newDay = "";
            //是否是节假日标志
            String jjrflag = "";
            String day = "";
            if (i < 10) {
                day = "0" + i;
                newDay = nyDate + "-" + day;
            } else {
                day = i + "";
                newDay = nyDate + "-" + day;
            }

            String request = DateUtils.request(newDay);
            if ("1".equals(request)) {
                String week = DateUtils.getWeek(DateUtils.parseDate(newDay));
                if ("星期六".equals(week)) {
                    jjrflag = "zl";
                }
                if ("星期日".equals(week)) {
                    jjrflag = "zr";
                }
            }
            if ("2".equals(request)) {
                jjrflag = "jjr";
            }
            NcAttendDetail ncAttendDetail ;
//         读取人事档案表获取员工信息
            NcPersonnelInfo ncPersonnelInfo = new NcPersonnelInfo();
            ncPersonnelInfo.setState("1");
            List<NcPersonnelInfo> personnelInfos = ncPersonnelInfoMapper.selectNcPersonnelInfoList(ncPersonnelInfo);
            NcSchedulingRules rules = new NcSchedulingRules();
            rules.setYear(split[0]);
            List<NcSchedulingRules> ruleInfos = ncSchedulingRulesMapper.selectRulesListCondition(rules);
            NcOriginalAttend originalAttend = new NcOriginalAttend();
            originalAttend.setNyDate(nyDate);
            List<NcOriginalAttend> ncOriginalAttendLists = originalAttendMapper.selectNcOriginalAttendList(originalAttend);
            if (ncOriginalAttendLists.size() == 0) {
                throw new RuntimeException("无本月考勤数据！");
            }
//            List<NcOriginalAttend> ncOriginalAttendList = ncOriginalAttendLists.stream().filter(v -> v.getAttendanceTime() != null).collect(Collectors.toList());
            for (NcPersonnelInfo person : personnelInfos) {
                NcWorkOverCalculate ncWorkOverCalculates = selectJbgzTime(person);
                ncAttendDetail = new NcAttendDetail();
                NcSchedulingRules result;
                int fz;
                String workType;
                int dkNum;//日打卡次数默认两次
                String description = "";
                double workLenth;//日工作时长
                int beforeWorkLen = Integer.parseInt(ncWorkOverCalculates.getBeforeWorkLen());//上班前加班
                int afterWorkLen = Integer.parseInt(ncWorkOverCalculates.getAfterWorkLen());//下班后加班

                if (ruleInfos.size() > 0) {
                    List<NcSchedulingRules> ruleInfo = ruleInfos.parallelStream().filter(rule -> person.getJobNo().equals(rule.getJobNo())).collect(Collectors.toList());
                    if (ruleInfo.size() > 0) {
                        dkNum = ruleInfo.get(0).getDkNum();
                        workLenth = ruleInfo.get(0).getWorkLength();
                        result = choosejjrInfo(ruleInfo, newDay);
                        if ("zl".equals(jjrflag)) {
                            NcHolidayManage ncHolidayManage = new NcHolidayManage();
                            ncHolidayManage.setYear(split[0]);
                            ncHolidayManage.setMonth(split[1]);
                            ncHolidayManage.setDay(day);
                            List<NcHolidayManage> ncHolidayManages = ncHolidayManageMapper.selectNcHolidayManageList(ncHolidayManage);
                            if (ncHolidayManages.size() > 0) {
                                String htype = ncHolidayManages.get(0).getHType();
                                if ("上班".equals(htype)) {
                                    workType = "班";
                                } else {
                                    workType = "休";
                                }
                            } else {
                                workType = result.getSaturday();
                            }
                        } else if ("zr".equals(jjrflag)) {
                            NcHolidayManage ncHolidayManage = new NcHolidayManage();
                            ncHolidayManage.setYear(split[0]);
                            ncHolidayManage.setMonth(split[1]);
                            ncHolidayManage.setDay(day);
                            List<NcHolidayManage> ncHolidayManages = ncHolidayManageMapper.selectNcHolidayManageList(ncHolidayManage);
                            if (ncHolidayManages.size() > 0) {
                                String htype = ncHolidayManages.get(0).getHType();
                                if ("上班".equals(htype)) {
                                    workType = "班";
                                } else {
                                    workType = "节假日休班";
                                }
                            } else {
                                workType = result.getSunday();
                            }
                        } else if ("jjr".equals(jjrflag)) {
                            NcHolidayManage ncHolidayManage = new NcHolidayManage();
                            ncHolidayManage.setYear(split[0]);
                            ncHolidayManage.setMonth(split[1]);
                            ncHolidayManage.setDay(day);
                            List<NcHolidayManage> ncHolidayManages = ncHolidayManageMapper.selectNcHolidayManageList(ncHolidayManage);
                            if (ncHolidayManages.size() > 0) {
                                String htype = ncHolidayManages.get(0).getHType();
                                if ("上班".equals(htype)) {
                                    workType = "班";
                                } else {
                                    workType = "节假日休班";
                                }
                            } else {
                                workType = result.getStatutoryHoliday();
                            }
                        } else {
                            NcHolidayManage ncHolidayManage = new NcHolidayManage();
                            ncHolidayManage.setYear(split[0]);
                            ncHolidayManage.setMonth(split[1]);
                            ncHolidayManage.setDay(day);
                            List<NcHolidayManage> ncHolidayManages = ncHolidayManageMapper.selectNcHolidayManageList(ncHolidayManage);
                            if (ncHolidayManages.size() > 0) {
                                String htype = ncHolidayManages.get(0).getHType();
                                if ("上班".equals(htype)) {
                                    workType = "班";
                                } else {
                                    workType = "节假日休班";
                                }
                            } else {
                                workType = "班";
                            }
                        }
                    } else {
                        throw new RuntimeException("请先设置" + person.getPName() + "的" + split[0] + "年的排班规则。");
                    }
                } else {
                    throw new RuntimeException("请先设置人员信息" + split[0] + "年的排班规则。");
                }
                String finalNewDay = newDay;
                List<NcOriginalAttend> originalRecord = ncOriginalAttendLists.parallelStream().filter(att -> {
                    String s = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, att.getDkTime());
                    return s.equals(finalNewDay) && att.getJobNum().equals(person.getJobNo());
                }).collect(Collectors.toList());
                if ("休".contains(workType)) {
                    if (originalRecord.size() == 0) {
                        ncAttendDetail.setJobNumber(person.getJobNo());
                        if("休".equals(workType)){
                            judge(ncAttendDetail, day, "周末休班");
                        }else{
                            judge(ncAttendDetail, day, "节假日休班");
                        }
                    } else {

                        if(originalRecord.size()==1){
                            ncAttendDetail.setJobNumber(person.getJobNo());
                            if("休".equals(workType)){
                                description = "周末加班缺卡";
                            }else{
                                description = "节假日加班缺卡";
                            }
                            judge(ncAttendDetail, day, description);

                        }else{
                            List<NcOriginalAttend> attends = new LinkedList<>();

                            Optional<NcOriginalAttend> min = originalRecord.stream()
                                    .min((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                            min.ifPresent((p -> {
                                attends.add(p);
                            }));

                            Optional<NcOriginalAttend> max = originalRecord.stream()
                                    .max((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                            max.ifPresent((p -> {
                                attends.add(p);
                            }));
                            String upDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(0).getDkTime());
                            String downDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(1).getDkTime());
                            String differenceMin = DateUtils.getDifferenceMin2(upDkTime, downDkTime);//下班超时时间


                            ncAttendDetail.setJobNumber(person.getJobNo());
                            if("休".equals(workType)){
                                judge(ncAttendDetail, day, "周末加班");
                                if(Double.parseDouble(differenceMin)>=workLenth){
                                    ncAttendDetail.setJbWeekend(1d);
                                }else{
                                    ncAttendDetail.setJbWeekend(Double.parseDouble(differenceMin)/60/workLenth);
                                }
                            }else{
                                judge(ncAttendDetail, day, "节假日加班");
                                if(Double.parseDouble(differenceMin)>=workLenth){
                                    ncAttendDetail.setJbWeekend(1d);
                                }else{
                                    ncAttendDetail.setJbWeekend(Double.parseDouble(differenceMin)/60/workLenth);
                                }
                            }
                        }
                    }
                } else {
                    //打卡次数为一天两次
                    if (dkNum == 2) {
                        String dkInfo = "";
                        //这是有打卡结果的打卡机
                        if (originalRecord == null || originalRecord.size() == 0) {
                            description = dkInfo = "缺勤";
                        } else {
                            String upRuleTime = result.getUpTime();
                            String downRuleTime = result.getDownTime();
                            if ("new".equals(type)) {
                                if (originalRecord.size() == 1) {
                                    double min = 0;
                                    String dkTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, originalRecord.get(0).getDkTime());
                                    String prefixTime = dkTime.split(" ")[1].split(":")[0];
                                    String dkResult = originalRecord.get(0).getDkResult();
                                    int prefixTimeInt = Integer.parseInt(prefixTime);
                                    if (prefixTimeInt < 12) {
                                        dkInfo = "上午" + dkResult + ",下午缺卡";
                                        String differenceupMin = DateUtils.getDifferenceMin2(dkTime,newDay+" "+upRuleTime);//上班早来时间
                                        fz = Integer.parseInt(differenceupMin);
                                        if (fz >= 10) {
                                            min = min+fz;
                                        }
                                    } else {
                                        //下午卡
                                        dkInfo = "上午缺卡,下午" + dkResult;
                                        String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, dkTime);//下班超时时间
                                        if (Double.parseDouble(differenceMin)>=0) {
                                            dkInfo = "上午缺卡,下午正常";
                                            fz = Integer.parseInt(differenceMin);
                                            if (fz >= 10) {
                                                min = min + fz;
                                            }
                                        }
                                    }
                                    ncAttendDetail.setJbMin(min);
                                } else if (originalRecord.size() == 2) {
                                    double min = 0;
                                    String dkResult1 = originalRecord.get(0).getDkResult();
                                    String dkResult2 = originalRecord.get(1).getDkResult();
                                    String sjdksj1 = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM,originalRecord.get(0).getDkTime());//实际上班打卡时间
                                    String sjdksj2 = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM, originalRecord.get(1).getDkTime());//实际下班打卡时间

                                    String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, sjdksj2);//下班超时时间
                                    String differenceupMin = DateUtils.getDifferenceMin2(sjdksj1,newDay+" "+upRuleTime);//上班早来时间

                                    if ("正常".equals(dkResult1) && "正常".equals(dkResult2)) {
                                        dkInfo = dkResult1;
                                    } else {
                                        dkInfo = "上午" + dkResult1 + ",下午" + dkResult2;
                                    }

                                    fz = Integer.parseInt(differenceMin);
                                    if (fz >= 10) {
                                        min = min + fz;
                                    }
                                    fz = Integer.parseInt(differenceupMin);
                                    if (fz >= 10) {
                                        min = min + fz;
                                    }
                                    ncAttendDetail.setJbMin(min);
                                } else {
                                    double minu = 0;

                                    List<NcOriginalAttend> attends = new LinkedList<>();

                                    Optional<NcOriginalAttend> min = originalRecord.stream()
                                            .min((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                                    min.ifPresent((p -> {
                                        attends.add(p);
                                    }));

                                    Optional<NcOriginalAttend> max = originalRecord.stream()
                                            .max((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                                    max.ifPresent((p -> {
                                        attends.add(p);
                                    }));
                                    //--------------------------------------------------------------
                                    String upDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(0).getDkTime());
                                    String downDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(1).getDkTime());
                                    String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, downDkTime);
                                    String differenceupMin = DateUtils.getDifferenceMin2(upDkTime,newDay+" "+upRuleTime);

                                    if (Double.parseDouble(differenceupMin)>=0) {
                                        fz = Integer.parseInt(differenceupMin);
                                        if (fz >= 10) {
                                            minu = minu + fz;
                                        }
                                        dkInfo = "上午正常";
                                    } else {
                                        dkInfo = "上午迟到";
                                    }
                                    if (Double.parseDouble(differenceMin)>=0) {
                                        dkInfo += ",下午正常";
                                        fz = Integer.parseInt(differenceMin);
                                        if (fz >= 10) {
                                            minu = minu + fz;
                                        }
                                    } else {
                                        dkInfo += ",下午早退";
                                    }
                                    if ("上午正常,下午正常".equals(dkInfo)) {
                                        dkInfo = "正常";
                                    }
                                    ncAttendDetail.setJbMin(minu);
                                }
                            } else {
                                //获得加班规则表分钟数
                                //旧式打卡机
                                if (originalRecord.size() == 1) {

                                    String dkTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, originalRecord.get(0).getDkTime());
                                    double min = 0;
                                    String hour = dkTime.split(" ")[1].split(":")[0];
                                    int hourInt = Integer.parseInt(hour);
                                    if (hourInt < 12) {
                                        String differenceupMin = DateUtils.getDifferenceMin2(dkTime,newDay+" "+upRuleTime);//上班早来时间
                                        fz = Integer.parseInt(differenceupMin);
                                        if (fz >= beforeWorkLen) {
                                            min = min+fz;
                                        }
                                        //上午卡
                                        if (Double.parseDouble(differenceupMin)>=0) {

                                            description ="下午缺卡";
                                            dkInfo = "上午正常,下午缺卡";
                                        } else {
                                            description = dkInfo = "上午迟到,下午缺卡";
                                        }
                                    } else {
                                        //下午卡
                                        String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, dkTime);//下班超时时间
                                        if (Double.parseDouble(differenceMin)>=0) {
                                             dkInfo = "上午缺卡,下午正常";
                                            description = "上午缺卡";
                                            fz = Integer.parseInt(differenceMin);
                                            if (fz >= afterWorkLen) {
                                                min = min+fz;
                                            }
                                        } else {
                                            description =  dkInfo = "上午缺卡,下午早退";
                                        }
                                    }
                                    ncAttendDetail.setJbMin(min);
                                } else if (originalRecord.size() == 2) {
                                    double min = 0;
                                    String upDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", originalRecord.get(0).getDkTime());
                                    String downDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", originalRecord.get(1).getDkTime());
                                    String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, downDkTime);//下班超时时间
                                    String differenceupMin = DateUtils.getDifferenceMin2(upDkTime,newDay+" "+upRuleTime);//上班早来时间
                                    if (Double.parseDouble(differenceupMin)>=0) {
                                        fz = Integer.parseInt(differenceupMin);
                                        if (fz >= beforeWorkLen) {
                                            min = min+fz;
                                        }
                                        dkInfo = "上午正常";
                                    } else {
                                        description =  dkInfo = "上午迟到";
                                    }
                                    if (Double.parseDouble(differenceMin)>=0) {
                                         dkInfo += ",下午正常";
                                        fz = Integer.parseInt(differenceMin);
                                        if (fz >= afterWorkLen) {
                                            min = min+fz;
                                        }
                                    } else {
                                        description =  dkInfo += ",下午早退";
                                    }
                                    ncAttendDetail.setJbMin(min);
                                } else {
                                    double minu = 0;
                                    List<NcOriginalAttend> attends = new LinkedList<>();

                                    Optional<NcOriginalAttend> min = originalRecord.stream()
                                            .min((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                                    min.ifPresent((p -> {
                                        attends.add(p);
                                    }));

                                    Optional<NcOriginalAttend> max = originalRecord.stream()
                                            .max((p1, p2) -> (int) (p1.getDkTime().getTime() - p2.getDkTime().getTime()));
                                    max.ifPresent((p -> {
                                        attends.add(p);
                                    }));
                                    //--------------------------------------------------------------
                                    String upDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(0).getDkTime());
                                    String downDkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", attends.get(1).getDkTime());
                                    String differenceMin = DateUtils.getDifferenceMin2(newDay+" "+downRuleTime, downDkTime);
                                    String differenceupMin = DateUtils.getDifferenceMin2(upDkTime,newDay+" "+upRuleTime);

                                    if (Double.parseDouble(differenceupMin)>=0) {
                                        fz = Integer.parseInt(differenceupMin);
                                        if (fz >= beforeWorkLen) {
                                            minu = minu + fz;
                                        }
                                        dkInfo = "上午正常";
                                    } else {
                                        description =  dkInfo = "上午迟到";
                                    }
                                    if (Double.parseDouble(differenceMin)>=0) {
                                        dkInfo += ",下午正常";
                                        fz = Integer.parseInt(differenceMin);
                                        if (fz >= afterWorkLen) {
                                            minu = minu + fz;
                                        }
                                    } else {
                                        description =  dkInfo += ",下午早退";
                                    }
                                    ncAttendDetail.setJbMin(minu);
                                }
                                if ("上午正常,下午正常".contains(dkInfo)) {
                                    dkInfo = "全天正常";
                                }
                            }
                        }
                        judge(ncAttendDetail, day, dkInfo);



                    }else if(dkNum==4){

                    }else{
                        throw new RuntimeException("暂不匹配"+dkNum+"次日打卡次数！");
                    }
                }

                ncAttendDetail.setJobNumber(person.getJobNo());
                NcAttendDetail ncAttendDetails = ncAttendDetailMapper.selectNcAttendDetailByJobNum(person.getJobNo());
                ncAttendDetail.setGcCode(person.getFactoryCode());
                ncAttendDetail.setGcName(person.getFactoryName());
                ncAttendDetail.setEmployeeName(person.getPName());
                ncAttendDetail.setDepartment(person.getDepartment());
                ncAttendDetail.setBranch(person.getBranch());
                ncAttendDetail.setZb(person.getPGroup());
                ncAttendDetail.setJobs(person.getJobs());
                ncAttendDetail.setNyDate(nyDate);
                if (ncAttendDetails == null) {
                    ncAttendDetailMapper.insertNcAttendDetail(ncAttendDetail);
                } else {
                    ncAttendDetail.setId(ncAttendDetails.getId());
                    if (ncAttendDetail.getJbMin() == null) {
                        ncAttendDetail.setJbMin(0D);
                    }
                    if (ncAttendDetail.getJbWeekend() == null) {
                        ncAttendDetail.setJbWeekend(0d);
                    }
                    if (ncAttendDetail.getJbJjr() == null) {
                        ncAttendDetail.setJbJjr(0d);
                    }

                    ncAttendDetail.setJbMin(ncAttendDetails.getJbMin() + ncAttendDetail.getJbMin());
                    ncAttendDetail.setJbWeekend(ncAttendDetails.getJbWeekend() + ncAttendDetail.getJbWeekend());
                    ncAttendDetail.setJbJjr(ncAttendDetails.getJbJjr() + ncAttendDetail.getJbJjr());
                    ncAttendDetailMapper.updateNcAttendDetail(ncAttendDetail);
                }


                if(StringUtils.isNotEmpty(description)){
                    NcAttendAbnormal ncAttendAbnormal = new NcAttendAbnormal();
                    ncAttendAbnormal.setGcCode(person.getFactoryCode());
                    ncAttendAbnormal.setGcName(person.getFactoryName());
                    ncAttendAbnormal.setEmployeeName(person.getPName());
                    ncAttendAbnormal.setJobNumber(person.getJobNo());
                    ncAttendAbnormal.setDepartment(person.getDepartment());
                    ncAttendAbnormal.setBranch(person.getBranch());
                    ncAttendAbnormal.setZb(person.getPGroup());
                    ncAttendAbnormal.setJobs(person.getJobs());
                    ncAttendAbnormal.setEntryDate(person.getEntryDate());
                    ncAttendAbnormal.setNyDate(nyDate);
                    ncAttendAbnormal.setAbnormalDate(newDay);
                    ncAttendAbnormal.setAbnormalDescrip(description);
                    ncAttendAbnormal.setCreateBy(SecurityUtils.getUsername());
                    ncAttendAbnormal.setCreateTime(new Date());
                    ncAttendAbnormalMapper.insertNcAttendAbnormal(ncAttendAbnormal);
                }

            }
        }
        return 1;
    }

    public void judge(NcAttendDetail ncAttendDetail, String dayNum, String dkResult) {
        if ("1".equals(dayNum) || "01".equals(dayNum)) {
            ncAttendDetail.setDay1(dkResult);
        } else if ("2".equals(dayNum) || "02".equals(dayNum)) {
            ncAttendDetail.setDay2(dkResult);
        } else if ("3".equals(dayNum) || "03".equals(dayNum)) {
            ncAttendDetail.setDay3(dkResult);
        } else if ("4".equals(dayNum) || "04".equals(dayNum)) {
            ncAttendDetail.setDay4(dkResult);
        } else if ("5".equals(dayNum) || "05".equals(dayNum)) {
            ncAttendDetail.setDay5(dkResult);
        } else if ("6".equals(dayNum) || "06".equals(dayNum)) {
            ncAttendDetail.setDay6(dkResult);
        } else if ("7".equals(dayNum) || "07".equals(dayNum)) {
            ncAttendDetail.setDay7(dkResult);
        } else if ("8".equals(dayNum) || "08".equals(dayNum)) {
            ncAttendDetail.setDay8(dkResult);
        } else if ("9".equals(dayNum) || "09".equals(dayNum)) {
            ncAttendDetail.setDay9(dkResult);
        } else if ("10".equals(dayNum)) {
            ncAttendDetail.setDay10(dkResult);
        } else if ("11".equals(dayNum)) {
            ncAttendDetail.setDay11(dkResult);
        } else if ("12".equals(dayNum)) {
            ncAttendDetail.setDay12(dkResult);
        } else if ("13".equals(dayNum)) {
            ncAttendDetail.setDay13(dkResult);
        } else if ("14".equals(dayNum)) {
            ncAttendDetail.setDay14(dkResult);
        } else if ("15".equals(dayNum)) {
            ncAttendDetail.setDay15(dkResult);
        } else if ("16".equals(dayNum)) {
            ncAttendDetail.setDay16(dkResult);
        } else if ("17".equals(dayNum)) {
            ncAttendDetail.setDay17(dkResult);
        } else if ("18".equals(dayNum)) {
            ncAttendDetail.setDay18(dkResult);
        } else if ("19".equals(dayNum)) {
            ncAttendDetail.setDay19(dkResult);
        } else if ("20".equals(dayNum)) {
            ncAttendDetail.setDay20(dkResult);
        } else if ("21".equals(dayNum)) {
            ncAttendDetail.setDay21(dkResult);
        } else if ("22".equals(dayNum)) {
            ncAttendDetail.setDay22(dkResult);
        } else if ("23".equals(dayNum)) {
            ncAttendDetail.setDay23(dkResult);
        } else if ("24".equals(dayNum)) {
            ncAttendDetail.setDay24(dkResult);
        } else if ("25".equals(dayNum)) {
            ncAttendDetail.setDay25(dkResult);
        } else if ("26".equals(dayNum)) {
            ncAttendDetail.setDay26(dkResult);
        } else if ("27".equals(dayNum)) {
            ncAttendDetail.setDay27(dkResult);
        } else if ("28".equals(dayNum)) {
            ncAttendDetail.setDay28(dkResult);
        } else if ("29".equals(dayNum)) {
            ncAttendDetail.setDay29(dkResult);
        } else if ("30".equals(dayNum)) {
            ncAttendDetail.setDay30(dkResult);
        } else if ("31".equals(dayNum)) {
            ncAttendDetail.setDay31(dkResult);
        }
    }

    public NcSchedulingRules choosejjrInfo(List<NcSchedulingRules> ruleInfo, String date) {
        String[] split = date.split("-");
        String year = split[0];
        String month = split[1];
        String day = split[2];
        NcSchedulingRules rules = new NcSchedulingRules();
        List<NcSchedulingRules> dayRules = ruleInfo.stream().filter(r ->
                r.getYear().equals(year) && r.getMonth().equals(month) && r.getDay().equals(day)
        ).collect(Collectors.toList());
        if (dayRules.size() == 0) {
            List<NcSchedulingRules> monthRules = ruleInfo.stream().filter(r ->
                    r.getYear().equals(year) && r.getMonth().equals(month)
            ).collect(Collectors.toList());
            if (monthRules.size() == 0) {
                List<NcSchedulingRules> yearRules = ruleInfo.stream().filter(r ->
                        r.getYear().equals(year)
                ).collect(Collectors.toList());
                rules = yearRules.get(0);
            } else {
                rules = monthRules.get(0);
            }
        } else {
            rules = dayRules.get(0);
        }
        return rules;
    }


    //获取加班规则时间
    public NcWorkOverCalculate selectJbgzTime(NcPersonnelInfo person){
        List<NcWorkOverCalculate> ncWorkOverCalculates;
        NcWorkOverCalculate ncWorkOverCalculate = new NcWorkOverCalculate();
        ncWorkOverCalculate.setJobNumber(person.getJobNo());
        ncWorkOverCalculates = ncWorkOverCalculateMapper.selectNcWorkOverCalculateList(ncWorkOverCalculate);
        if(ncWorkOverCalculates.size()==0){
            ncWorkOverCalculate.setDepartment(person.getDepartment());
            ncWorkOverCalculates = ncWorkOverCalculateMapper.selectNcWorkOverCalculateList(ncWorkOverCalculate);
            if(ncWorkOverCalculates.size() == 0){
                ncWorkOverCalculate.setGcCode(person.getFactoryCode());
                ncWorkOverCalculates = ncWorkOverCalculateMapper.selectNcWorkOverCalculateList(ncWorkOverCalculate);
                if(ncWorkOverCalculates.size()==0){
                    throw new RuntimeException("请设置加班规则！");
                }
            }
        }
        return ncWorkOverCalculates.get(0);
    }



    public void dkCompare4(List<NcOriginalAttend> oAttenRecord,String ruleTime,String sjdkTime,String type){
        String result = "";
        if("new".equals(type)){
            if(oAttenRecord.size()==1){
                String dkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(0).getDkTime());
                String HH = dkTime.split(" ")[1].split(":")[0];
                int HHInt = Integer.parseInt(HH);
                if(HHInt<=12){

                }else{

                }
            }else if(oAttenRecord.size()==2){
                String dkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(0).getDkTime());
                String dkTime1 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(1).getDkTime());
            }else if(oAttenRecord.size()==3){
                String dkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(0).getDkTime());
                String dkTime1 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(1).getDkTime());
                String dkTime2 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(2).getDkTime());
            }else if(oAttenRecord.size()==4){
                String dkTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(0).getDkTime());
                String dkTime1 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(1).getDkTime());
                String dkTime2 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(2).getDkTime());
                String dkTime3 = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", oAttenRecord.get(3).getDkTime());
            }else{
                result = "无法识别";
            }
        }else{
            if(oAttenRecord.size()==1){

            }else if(oAttenRecord.size()==2){

            }else if(oAttenRecord.size()==3){

            }else if(oAttenRecord.size()==4){

            }else{
                result = "无法识别";
            }
        }
    }

}
